package com.xxh.market.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.xxh.market.dao.OrderDao;
import com.xxh.market.dao.OrderDetail;
import com.xxh.market.dao.impl.OrderDaoImpl;
import com.xxh.market.dao.impl.OrderDetailimpl;
import com.xxh.market.entity.Order;
import com.xxh.market.entity.Orderdetail;
import com.xxh.market.service.OrderService;

import java.util.List;

public class OrderServiceImpl implements OrderService {
    //引入dao层
    OrderDao orderDao=new OrderDaoImpl();
    OrderDetail orderDetailDao=new OrderDetailimpl();

    @Override
    public void addOrder(int uid, List<Integer> bookList, double totalPrice) {
        //生成一条order记录
        Order order = new Order();
        DateTime date = DateUtil.date();
        order.setOrdertime(date);
        order.setUid(uid);
        order.setPrice(totalPrice);
        //拿到当前时间
        String now = DateUtil.today();
        //转为指定的格式
        String parseStr = now.replace("-", "");
        String orderno=parseStr+uid;
        //生成10以内随机数
        for(int i=0;i<4;i++){
            int anInt = RandomUtil.randomInt(10);
            orderno+=anInt;
        }
        order.setOrderno(orderno);

        //插入后返回订单表的ID
        int orderID = orderDao.insertOrder(order);

        //构造订单详情
        for(int b:bookList){
            Orderdetail orderdetail = new Orderdetail();
            orderdetail.setOid(orderID);
            orderdetail.setBid(b);
            orderDetailDao.insertOrderDetail(orderdetail);
        }

    }
}
